﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.IdentityModel.Tokens;
using System.Security.Claims;

namespace Ys.Base.Common.Identity
{
    /// <summary>
    /// Jwt Token提供类
    /// </summary>
    public class TokenProvider
    {

       /// <summary>
       /// JWT认证Token提供类型
       /// </summary>
       /// <param name="jwtTokenProviderOptions"></param>
        public TokenProvider(JwtTokenProviderOptions jwtTokenProviderOptions)
        {
            Jwtoptions = jwtTokenProviderOptions;

        }
        /// <summary>
        /// Jwt发行者身份配置
        /// </summary>
        public JwtTokenProviderOptions Jwtoptions { get; set; }

        /// <summary>
        ///根据数据创建令牌
        /// </summary>
        /// <param name="TokenName">TokenName </param>
        /// <param name="ClaimList">ClaimList</param>
        /// <param name="data">data</param>
        /// <param name="isEncrypt">isEncrypt</param>
        /// <returns></returns>
        public async Task<JwtTokenEntity> CreateToken(string TokenName, List<Claim> ClaimList, object data = null, bool isEncrypt = false)
        {
            try
            {
                var identity = await GetIdentity(TokenName);
                if (identity == null)
                {
                    return null;
                }

                //创建令牌
                //声明令牌
                var now = DateTime.Now;
                var claims = ClaimList;
                var jwt = new JwtSecurityToken(
                    issuer: Jwtoptions.Issuer
                    , audience: Jwtoptions.Audience
                     , claims: claims
                    , notBefore: now
                    , expires: now.Add(Jwtoptions.Expiration)
                    , signingCredentials: Jwtoptions.SigningCredentials
                    );


                //生成令牌
                var enclderjwt = new JwtSecurityTokenHandler().WriteToken(jwt);
                var response = new JwtTokenEntity
                {
                    Status = true
                ,
                    code = 200
                ,
                    message = "token授权成功"
                ,
                    Token = enclderjwt
                ,
                    Data = data
                ,
                    expires_in = (int)Jwtoptions.Expiration.TotalMinutes
                };


                return response;
            }
            catch (Exception ex)
            {
                var messsage = ex.Message;
                return new JwtTokenEntity { code = (int)400, message = ex.Message, Status = false };
            }
        }


        /// <summary>
        /// 查询令牌是否存在
        /// </summary>
        /// <param name="TokenKeyName"></param>
        /// <returns></returns>
        public Task<ClaimsIdentity> GetIdentity(string TokenKeyName)
        {
            return Task.FromResult(new ClaimsIdentity(new System.Security.Principal.GenericIdentity(TokenKeyName, "token"),
                new Claim[] {
                    new Claim(ClaimTypes.Name,TokenKeyName)
                }));
        }



    }
}
